package com.dfrz.stockMarket.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dfrz.stockMarket.bean.MessageBean;
import com.dfrz.stockMarket.cons.Constants;
import com.dfrz.stockMarket.model.StocksModel;
import com.dfrz.stockMarket.model.UserInfoModel;

public class UserInfoDao extends BaseDao{
/*	public static void main(String[] args) {
		try {
			System.out.println(getUserInfoByName("1111").getMsg());
			UserInfoDao userInfoDao = new UserInfoDao();
			UserInfoModel userInfoModel = (UserInfoModel) userInfoDao.getUserInfoByName("1111").getMsg();
			
			int userID = userInfoModel.getUserID();
			System.out.println("userid--"+userID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		UserInfoModel usermodel=new UserInfoModel("0000", "0000");
		try {
			save(usermodel);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	public static MessageBean getUserInfoByName(String name) throws Exception {
		
		UserInfoModel userInfoModel = new UserInfoModel();
	
		Connection conn=returncon();
		//获取stmt
		PreparedStatement stmt = conn.prepareStatement(" select userid, username, userpassword from sto_userinfo where username=?");
		stmt.setString(1, name);

		ResultSet rs = stmt.executeQuery();
		while(rs.next()){
			int userid = rs.getInt("userid");
			String userpassword = rs.getString("userpassword");
			String username = rs.getString("username");
			System.out.println(userpassword);
		
			userInfoModel.setUserID(userid);
			userInfoModel.setUserName(username);
			userInfoModel.setUserPassWord(userpassword);
		}
		
		//提交or回滚
		conn.commit();
//		conn.rollback();
		//释放stmt conn
		rs.close();
		stmt.close();
		conn.close();
		
		return new MessageBean(true, userInfoModel);
	}
	public static MessageBean save(UserInfoModel userInfoModel) throws Exception {
		Connection conn=returncon();
		PreparedStatement stmt = conn.prepareStatement("insert into sto_userinfo(username, userpassword)values (?, ?)");
		stmt.setString(1, userInfoModel.getUserName());
		stmt.setString(2, userInfoModel.getUserPassWord());
		int ret = stmt.executeUpdate();
		conn.commit();
		stmt.close();
		conn.close();
		return new MessageBean(true, Constants.USER_SUCCESS_TYPE1);
	}
	
public static MessageBean getAllUser() throws Exception {		
		
		List<UserInfoModel> list = new ArrayList<UserInfoModel>();
		
		UserInfoModel userInfoModel = new UserInfoModel();
		
		Connection conn=returncon();
		//获取stmt
		PreparedStatement stmt = conn.prepareStatement(" select username, userpassword from sto_userinfo");
	

		ResultSet rs = stmt.executeQuery();
		while(rs.next()){
			String userpassword = rs.getString("userpassword");
			String username = rs.getString("username");
		
			userInfoModel.setUserName(username);
			userInfoModel.setUserPassWord(userpassword);
			
			list.add(userInfoModel);
		}
		//提交or回滚
	
//		conn.rollback();
		//释放stmt conn
		rs.close();
		stmt.close();
		conn.close();
		return new MessageBean(true, list);
	}
	
	
	public static MessageBean getStockByID(int ID) {		
		StocksModel stocksModel =new StocksModel();
		return new MessageBean(true, stocksModel);
	}
	
	public static MessageBean getSizeByID(int ID) {
		int size = 10;
		return new MessageBean(true, size);
	}
	
	public static MessageBean getAllSize() {
		int size = 10;
		return new MessageBean(true, size);
	}
}
